package com.yang.service.impl;

import com.mysql.jdbc.Security;
import com.yang.dao.RegisterDao;
import com.yang.entity.UserEntity;
import com.yang.entity.VerificationCodeEntity;
import com.yang.service.RegisterService;
import com.yang.service.SendEmailService;
import com.yang.util.PublicUtil;
import com.yang.util.SecurityUtil;
import com.yang.util.SnowFlake;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {

    private static Logger logger = Logger.getLogger(RegisterServiceImpl.class);

    @Resource
    RegisterDao registerDao;
    @Resource
    SendEmailService sendEmailService;

    @Override
    public Map<String,Object> getCode(String QQEmil) {

        Map<String,Object> map = new HashMap<String,Object>();
        //已经存在的用户不发
        if(!registerDao.judgeEmilExist(QQEmil)) {
            logger.error("邮箱号已经存在用户");
            map.put("info","邮箱号已经存在用户");
            return map;
        }
        //验证码表中存在删不去的不发
        if(!registerDao.deleteCode(QQEmil)){
            logger.error("验证码表中存在记录不能删去");
            map.put("info","网络错误，请重新刷新");
            return map;
        }
        //生成验证码
        String code = PublicUtil.getSixNum();
        //获取当前时间
        long time = System.currentTimeMillis();
        //获取id
        SnowFlake snowFlake = new SnowFlake(2,1);
        long id = snowFlake.getId();
        logger.info("自动生成的id =  "+id+"   cod =  "+ "    time =   "+time);
        VerificationCodeEntity verificationCodeEntity = new VerificationCodeEntity(id,QQEmil,code,time);

        //保存不了的不发
        if(!registerDao.saveCode(verificationCodeEntity)){
            logger.error("验证码保存失败");
            map.put("info","网络错误，请重新刷新");
            return map;
        }
        try {
            sendEmailService.sendEmil(code,QQEmil);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("验证码发送失败");
            map.put("info","验证码发送失败");
            return map;
        }
        logger.info("验证码内容"+verificationCodeEntity.toString());
        map.put("info","验证码发送成功，有效时间为两分钟！");
        return map;
    }

    @Override
    public Map<String, Object> register(String QQEmil, String userName, String password, String code) {
        Map<String,Object> map = new HashMap<String, Object>();
        VerificationCodeEntity verificationCodeEntity = registerDao.findVerificationCode(QQEmil);
        if(verificationCodeEntity == null){
            map.put("info","请先发送验证码");
            logger.error("未找到该邮箱");
            return map;
        }
        if(!verificationCodeEntity.getCode().equals(code)){
            map.put("info","验证码不正确");
            logger.error("验证码匹配失败");
            return map;
        }
        if(!PublicUtil.judgeCodeTime(verificationCodeEntity.getTime())){
            map.put("info","认证过期");
            logger.error("验证码过期!");
            return map;
        }
        //删除验证码
        registerDao.deleteCode(QQEmil);
        SnowFlake snowFlake = new SnowFlake(3,1);
        long id = snowFlake.getId();
        try {
            password = SecurityUtil.encryptPassword(password);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("密码加密失败！");
        }
        String identification = "0";
        String imgUrl = null;
        UserEntity userEntity = new UserEntity(id,QQEmil,password,userName,imgUrl,identification);
        if(!registerDao.saveUser(userEntity)){
            map.put("info","网络出错！");
            return map;
        }
        map.put("info","注册成功");
        return map;
    }
}
